package com.hotel.service.impl;

import com.hotel.mapper.UserMapper;
import com.hotel.pojo.User;
import com.hotel.service.UserService;
import com.hotel.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl  implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public String login(@RequestBody User user) {
        String password = user.getPassword();
        String md5Pass = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(md5Pass);
        User userDB = userMapper.findUserByUP(user);
        if (userDB == null){
            return null;
        }
        String uuid = UUID.randomUUID().toString().replace("-", "");
        return uuid;
    }

    @Override
    public PageResult getEmpList(PageResult pageResult) {
        Long total = userMapper.getTotal();
        int size = pageResult.getPageSize();
        int start = (pageResult.getPageNum() - 1) * size;
        String query = pageResult.getQuery();
        List<User> rows = userMapper.findEmpListByPage(start,size,query);
        return pageResult.setTotal(total).setRows(rows);
    }

    @Override
    public User findEmpById(Integer userId) {
        return userMapper.findEmpById(userId);
    }

    @Override
    public void delete(Integer userId) {
        userMapper.delete(userId);
    }

    @Override
    public void addEmp(User user) {
        //1.密码加密处理
        Date date = new Date();
        String md5Pass = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(md5Pass)
                .setStatus(true).
                setCreated(date)
                .setUpdated(date);//最好保证时间唯一性
        userMapper.addEmp(user);
    }

    @Override
    public void updateEmp(User user) {
        userMapper.updateEmp(user);
    }

    @Override
    public void addUser(User user) {
        //1.密码加密处理
        Date date = new Date();
        String md5Pass = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(md5Pass)
                .setStatus(true).
                setCreated(date)
                .setUpdated(date);//最好保证时间唯一性
        userMapper.addUser(user);
    }

    @Override
    public void updateStatus(User user) {
        userMapper.updateStatus(user);
    }
}
